<?php
namespace App\HttpController;


class Admin extends \App\Base\AuthController
{

    public function __construct(){
        parent::__construct();
        $this->moduleid=1;
        $this->MD=$this->CORE_CONF->mod_single($this->moduleid);
		\copy_setting($this->moduleid);
        $this->setting=\load_setting($this->moduleid);
        $this->getView()->assign(['MODULEID'=>$this->moduleid,'MD'=>$this->MD]);
    }

    public function index()
    {
        $this->response()->write('admin/index');
    }

    public function safe(){
        $request=$this->request();
        $post = $request->getParsedBody();
        if($post){
            $old_password=trim($post['old_password']);
            $new_password=trim($post['new_password']);
            $new_password2=trim($post['new_password2']);
            if(!$old_password or !$new_password or !$new_password2){
                $this->msg(0,'请填写完密码后再提交');
                return;
            }
            if($new_password!=$new_password2){
                $this->msg(0,'两次新密码不一致');
                return;
            }
            $userid=$this->auth['userid'];
            $model = new \App\Model\MemberModel();
            $user = $model->where(['userid'=>$userid])->field('password')->get();
            if(md5($old_password)!=$user['password']){
                $this->msg(0,'旧密码不正确');
                return;
            }

            $model = \App\Model\MemberModel::create()->get($userid);
            $model->update(['password'=>md5($new_password)]);
            $affected_rows=$model->lastQueryResult()->getAffectedRows();

            $this->msg($affected_rows);
            return;
        }

        $this->fetch(__FUNCTION__,[]);
    }

    public function setting(){
        $request=$this->request();
        $post = $request->getParsedBody();
        if($post){
            $r=\save_setting($this->moduleid,$post);
			$this->msg($r);
        }
        $setting=\load_setting($this->moduleid);
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

}